package com.mb.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mb.common.config.CommonConfig;
import com.mb.common.utils.Base64Utils;
import com.mb.common.utils.HttpClientUtil;
import com.mb.pay.service.IPayService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付处理实现
 *
 * @author
 * @create 2018-03-31 上午11:22
 **/
@Service("payService")
public class PayServiceImpl implements IPayService {
    private static final Logger logger = LoggerFactory.getLogger(PayServiceImpl.class);

    @Autowired
    private CommonConfig commonConfig;

    /**
     * 退款
     *
     * @return
     */
    @Override
    public JSONObject RefundPay(Map<String,Object> map) {
        JSONObject payData = new JSONObject();
        String returnOrderUrl = commonConfig.getRefundOrderUrl();
        //支付方式

        JSONObject payReqData = new JSONObject();
        payReqData.put("payOrderId", map.get("payOrderId"));
        payReqData.put("payMode", map.get("payMode"));
        payReqData.put("refundAmt", map.get("refundAmt"));
        payReqData.put("returnOrderId",map.get("returnOrderId"));

        Map<String, String> params = new HashMap<>();
        params.put("reqData", Base64Utils.encode(payReqData.toJSONString()));
        logger.info("returnOrderUrl :" + returnOrderUrl);
        String ret = HttpClientUtil.doPost(returnOrderUrl, params);
        logger.info("returnOrderUrl http ret :" + ret);

        if (StringUtils.isNotEmpty(ret)) {
            String jsonStr = Base64Utils.decode(ret);
            payData = JSONObject.parseObject(jsonStr);
        }
        return payData;
    }

}
